<?php

namespace App\Store\Controllers\Financial\Goods;

use App\Common\Services\CommonExcelExport;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Goods\GoodsAllotInfolist;
use App\Store\Models\Goods\GoodsReplenishInfolist;
use App\Store\Models\Goods\GoodsReturnInfolist;
use App\Store\Models\Goods\GoodsStockinInfolist;
use App\Store\Models\Goods\GoodsStockList;
use App\Store\Models\OldMaterial\Stock;
use App\Store\Models\Order\OrderRecoveryGoods;
use App\Store\Models\Order\OrderSalesGoods;
use App\Store\Models\Order\OrderSalesRecoveryGoods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Account\SiteStore;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Maatwebsite\Excel\Facades\Excel;
use Mockery\Exception;

class GoodsPurchaseSalesController extends Controller
{
    public function getlist(Request $request)
    {
        $data = $this->getdata($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $data]);
    }

    public function getdatabyymd($request, $ymd, $where, $goods_category_ids, $store_id)
    {
        $table_name = (new GoodsStockList())->getTable() . '_' . $ymd;

        if (!Schema::hasTable($table_name)) {
            return [];
        }

        $query = Db::table($table_name)->where($where);
        if (!empty($goods_category_ids)) {
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        if (!empty($store_id)) {
            $query->whereIn('store_id', $store_id);
        }

        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if ($store_ids) { //权限控制
            if (is_array($store_ids)) { //获取当前权限下的门店
                $query->whereIn('store_id', $store_ids);
            }
            if (is_numeric($store_ids) && intval($store_ids) > 0) { //获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        $query->select('goods_category_id', 'pricetype', DB::raw('sum(count) as count, sum(count*piece_weight) as total_piece_weight, sum(total_netglod_weight) as total_netglod_weight, sum(count*label_price) as total_label_price, sum(total_cost_fee) as total_cost'));
        $list = $query->groupBy('goods_category_id')->get()->toArray();

        $data = [];
        foreach ($list as $k => $v) {
            $data[$k] = json_decode(json_encode($v, true), true);

//            $is_gram = GoodsCategory::where(['id' => $v->goods_category_id])->value('is_gram');
//            if ($is_gram == 2) {
//                $data[$k]['total_cost'] = $v->total_cost_2;
//            } else {
//                $data[$k]['total_cost'] = $v->total_cost_1;
//            }
        }
        return $data;
    }

    public function getdata(Request $request)
    {
        $start_time = (!is_numeric($request->start_time) && strtotime($request->start_time) > 0) ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = (!is_numeric($request->end_time) && strtotime($request->end_time) > 0) ? strtotime($request->end_time) : intval($request->end_time);
        $start_time = strtotime(date('Y-m-d', $start_time) . ' 00:00:00');
        $end_time = strtotime(date('Y-m-d', $end_time) . ' 23:59:59');
        $request_store_ids = $request['store_ids'] ?? [];
        if (empty($request_store_ids)) {
            $store_ids = StoreCommon::getCurrentAccountStoreId($request);
            $store_ids = is_array($store_ids) ? $store_ids : ($store_ids>0?[$store_ids]:[]);
        } else {
            $store_ids = $request_store_ids;
        }

        if ($start_time == 0 || $end_time == 0) {
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => []]);
        }

        $where = [];

        $where['is_del']=1;
        if (!empty($request['goods_brand_id'])) {
            $where['goods_brand_id'] = $request['goods_brand_id'];
        }

        if (!empty($request['goods_style_id'])) {
            $where['goods_style_id'] = $request['goods_style_id'];
        }

        //多选类别
        $goods_category_id = $request['goods_category_ids'] ?? '';
        $goods_category_ids = [];
        if ($goods_category_id) {
            $goods_category_ids = is_array($goods_category_id) ? $goods_category_id : (is_array(json_decode($goods_category_id, true)) ? json_decode($goods_category_id, true) : []);
        }


        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        if ($accountinfo['type'] == 11) {
            $training = StoreCommon::gettraining($request);
            if ($training['training_min'] == 0 && $training['training_max'] == 0) {
                return response()->json(['code' => '001', 'msg' => '培训账号，请先设置公司培训上下限']);
            }
        }

        //期初库存
        $start = date('Ymd', $start_time);
        $res = $this->getdatabyymd($request, $start, $where, $goods_category_ids, $store_ids);
        $starts = [];
        if ($res) {
            foreach ($res as $v) {
                $starts[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
                $starts[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
                $starts[$v['goods_category_id']]['start'] = $v;
            }
        }

        //期末库存
        $end = date('Ymd', $end_time);
        $res = $this->getdatabyymd($request, $end, $where, $goods_category_ids, $store_ids);
        $ends = [];
        if ($res) {
            foreach ($res as $v) {
                $ends[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
                $ends[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
                $ends[$v['goods_category_id']]['end'] = $v;
            }
        }

        //采购入库
        if ($accountinfo['type'] == 11) {
            $stockin_where = $where + ['is_training' => 2];
        } else {
            $stockin_where = $where;
        }
        $stockin_where['audit_status']=2;
        unset($stockin_where['is_del']);
        $query = GoodsStockinInfolist::where($stockin_where);
        if (!empty($goods_category_ids)) {
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('stockin_at', '>=', $start_time)->where('stockin_at', '<=', $end_time);
        }

        $query->whereIn('store_id', $store_ids);

        $query->select('goods_category_id', DB::raw('sum(count) as count, sum(count*piece_weight) as total_piece_weight, sum(total_netglod_weight) as total_netglod_weight, sum(count*label_price) as total_label_price, sum(total_cost_fee) as total_cost '));
        $res = $query->groupBy('goods_category_id')->get()->toArray();
        $stock_ins = [];
        foreach ($res as $v) {
            $stock_ins[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
            $stock_ins[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
            $stock_ins[$v['goods_category_id']]['stock_in'] = $v;

//            $is_gram = GoodsCategory::where(['id' => $v['goods_category_id']])->value('is_gram');
//            if ($is_gram == 2) {
//                $stock_ins[$v['goods_category_id']]['stock_in']['total_cost'] = $v['total_cost_2'];
//            } else {
//                $stock_ins[$v['goods_category_id']]['stock_in']['total_cost'] = $v['total_cost_1'];
//            }
        }

        //调拨入库
        $query = GoodsAllotInfolist::query()->where($where)->where('audit_status',2);
        if (!empty($goods_category_ids)) {
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        if (!empty($store_ids)) {
            $query->whereIn('target_store_id', $store_ids);
        }

        $query->select('target_category_id as goods_category_id', DB::raw('sum(count_new) as count, sum(count_new*piece_weight) as total_piece_weight, sum(total_netglod_weight) as total_netglod_weight, sum(count_new*label_price) as total_label_price, sum(total_cost_fee) as total_cost'));
        $res = $query->groupBy('target_category_id')->get()->toArray();
        $allot_ins = [];
        foreach ($res as $v) {
            $allot_ins[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
            $allot_ins[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
            $allot_ins[$v['goods_category_id']]['allot_in'] = $v;

//            $is_gram = GoodsCategory::where(['id' => $v['goods_category_id']])->value('is_gram');
//            if ($is_gram == 2) {
//                $allot_ins[$v['goods_category_id']]['allot_in']['total_cost'] = $v['total_cost_2'] ?? '';
//            } else {
//                $allot_ins[$v['goods_category_id']]['allot_in']['total_cost'] = $v['total_cost_1'] ?? '';
//            }
        }


        //调拨出库=调拨出库+转旧料
        $query = GoodsAllotInfolist::where($where)->where('audit_status',2);
        if (!empty($goods_category_ids)) {
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        if (!empty($store_ids)) {
            $query->whereIn('start_store_id', $store_ids);
        }

        $query->select('goods_category_id', DB::raw('sum(count_new) as count, sum(count_new*piece_weight) as total_piece_weight, sum(total_cost_fee) as total_cost'));
        $res = $query->groupBy('goods_category_id')->get()->toArray();

        // 商品转旧料 加入到调拨出库
        $oldGoods = Stock::query()
            ->where('type', 3)
            ->where('is_del', 1)
            ->where('audit_status', 2)
            ->where($where)
            ->when(!empty($store_ids) && is_array($store_ids), function ($query) use ($store_ids) {
                $query->whereIn('store_id', $store_ids);
            })
            ->when($start_time > 0 && $end_time > $start_time, function ($query) use ($start_time,$end_time) {
                $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
            })
            ->select('goods_category_id', DB::raw('sum(count) as count, sum(count*piece_weight) as total_piece_weight, sum(count*netglod_weight) as total_netglod_weight, sum(count*label_price) as total_label_price, 0 as total_cost'))
            ->groupBy('goods_category_id')->get()->toArray();

        $res = array_merge($res, $oldGoods);
        $allot_outs = [];
        foreach ($res as $v) {
            $allot_outs[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
            $allot_outs[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
            $allot_outs[$v['goods_category_id']]['allot_out'] = $v;
//           $is_gram = GoodsCategory::where(['id' => $v['goods_category_id']])->value('is_gram');
//            if ($is_gram == 2) {
//                $allot_outs[$v['goods_category_id']]['allot_out']['total_cost'] = $v['total_cost_2'] ?? '';
//            } else {
//                $allot_outs[$v['goods_category_id']]['allot_out']['total_cost'] = $v['total_cost_1'] ?? '';
//            }
        }

        //补货入库
        $query = GoodsReplenishInfolist::where($where)->where('audit_status',2);
        if (!empty($goods_category_ids)) {
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        if (!empty($store_ids)) {
            $query->whereIn('store_id', $store_ids);
        }

        $query->select('goods_category_id', DB::raw('sum(count) as count, sum(count*piece_weight) as total_piece_weight, sum(total_netglod_weight) as total_netglod_weight, sum(count*label_price) as total_label_price, sum(total_cost_fee) as total_cost'));
        $res = $query->groupBy('goods_category_id')->get()->toArray();
        $replenishs =[];
        foreach ($res as $v) {
            $replenishs[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
            $replenishs[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
            $replenishs[$v['goods_category_id']]['replenish'] = $v;
//            $is_gram = GoodsCategory::where(['id' => $v['goods_category_id']])->value('is_gram');
//            if ($is_gram == 2) {
//                $replenishs[$v['goods_category_id']]['replenish']['total_cost'] = $v['total_cost_2'];
//            } else {
//                $replenishs[$v['goods_category_id']]['replenish']['total_cost'] = $v['total_cost_1'];
//            }
        }

        //返厂出库
        $query = GoodsReturnInfolist::where($where)->where('audit_status',2);
        if (!empty($goods_category_ids)) {
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        if (!empty($store_ids)) {
            $query->whereIn('store_id', $store_ids);
        }

        $query->select('goods_category_id', DB::raw('sum(count) as count, sum(count*piece_weight) as total_piece_weight, sum(total_netglod_weight) as total_netglod_weight, sum(count*label_price) as total_label_price, sum(total_cost_fee) as total_cost'));
        $res = $query->groupBy('goods_category_id')->get()->toArray();
        $returns=[];
        foreach ($res as $v) {
            $returns[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
            $returns[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
            $returns[$v['goods_category_id']]['return'] = $v;

//            $is_gram = GoodsCategory::where(['id' => $v['goods_category_id']])->value('is_gram');
//            if ($is_gram == 2) {
//                $returns[$v['goods_category_id']]['return']['total_cost'] = $v['total_cost_2'];
//            } else {
//                $returns[$v['goods_category_id']]['return']['total_cost'] = $v['total_cost_1'];
//            }
        }

        //销售出库
        if ($accountinfo['type'] == 11) {
            $sales_where = $where + [
                    ['total_money', '>', $training['training_min']],
                    ['total_money', '<', $training['training_max']],
                ];
        } else {
            $sales_where = $where;
        }
        $query = OrderSalesGoods::where($sales_where + [
                'red_status' => 1,
                'audit_status' => 2
            ]);
        if (!empty($goods_category_ids)) {
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }


        if (!empty($store_ids)) {
            $query->whereIn('store_id', $store_ids);
        }

        $query->select('goods_category_id', 'cost_fee', DB::raw('sum(count) as count, SUM(CASE WHEN is_gram = 2 THEN weight ELSE 0 END) as total_piece_weight ,SUM(CASE WHEN is_gram = 1 THEN weight ELSE 0 END) as total_netglod_weight, sum(count*(label_price)) as total_label_price,sum(total_money) as total_money, sum(real_total_money) as real_total_money'));
        $res = $query->groupBy('goods_category_id')->get()->toArray();
        $sales_arr=[];
        foreach ($res as $v) {
            $sales_arr[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
            $sales_arr[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
            $sales_arr[$v['goods_category_id']]['sales'] = $v;
            $sales_arr[$v['goods_category_id']]['sales']['total_cost'] = $v['cost_fee'];
        }

        //上柜
        if ($accountinfo['type'] == 11) {
            $recovery_where = $where + [
                    ['money', '>', $training['training_min']],
                    ['money', '<', $training['training_max']],
                ];
        } else {
            $recovery_where = $where;
        }
        $query1 = OrderRecoveryGoods::where($recovery_where + [
                'change_type' => 2,
                'audit_status' => 2
            ]);
        if (!empty($goods_category_ids)) {
            $query1->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query1->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        if (!empty($store_ids)) {
            $query1->whereIn('store_id', $store_ids);
        }

        $query1->select('goods_category_id', DB::raw('sum(cost_fee) as total_fee,sum(count) as count, SUM(CASE WHEN recovery_type = 2 THEN weight ELSE 0 END) as total_piece_weight ,SUM(CASE WHEN recovery_type = 1 THEN weight ELSE 0 END) as total_netglod_weight,sum(d_price) as total_label_price'));
        $query2 = OrderSalesRecoveryGoods::where($where + [
                'change_type' => 2,
                'audit_status' => 2
            ]);
        if (!empty($goods_category_ids)) {
            $query2->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query2->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }
        if (!empty($store_ids)) {
            $query2->whereIn('store_id', $store_ids);
        }

        $query2->select('goods_category_id', DB::raw('sum(cost_fee)as total_fee,sum(count) as count, SUM(CASE WHEN recovery_type = 2 THEN weight ELSE 0 END) as total_piece_weight ,SUM(CASE WHEN recovery_type = 1 THEN weight ELSE 0 END)  as total_netglod_weight,sum(d_price) as total_label_price'));
        $shanggui = $query1->union($query2)->groupBy('goods_category_id')->get()->toArray();



        // 旧料上柜参与统计
        $oldwhere = $where;
        $stock = Stock::query()
            ->where('stock_status', 6)
            ->where('audit_status',2)
            ->where($oldwhere)
            ->when(!empty($store_ids) && is_array($store_ids), function ($query) use ($store_ids) {
                $query->whereIn('store_id', $store_ids);
            })
            ->when($start_time > 0 && $end_time > $start_time, function ($query) use ($start_time,$end_time) {
                $query->where('update_at', '>=', $start_time)->where('update_at', '<=', $end_time);
            })
            ->select('goods_category_id', DB::raw('0 as total_cost,sum(count) as count, sum(piece_weight) as total_piece_weight, sum(netglod_weight) as total_netglod_weight,sum(label_price)/100 as total_label_price'))
            ->groupBy('goods_category_id')->get()->toArray();

        $shanggui = array_merge($shanggui, $stock);
        $oncounter_arr=[];
        foreach ($shanggui as $v) {
            if (!empty($v['goods_category_id'])) {
                $oncounter_arr[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
                $oncounter_arr[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
                $oncounter_arr[$v['goods_category_id']]['oncounter'] = $v;
            }

        }

        //删除
        $delwhere =$where;
        $delwhere['is_del']=2;
        $query = GoodsStockList::query()->where($delwhere);
        if (!empty($goods_category_ids)) {
            $query->whereIn('goods_category_id', $goods_category_ids);
        }
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }


        if (!empty($store_ids)) {
            $query->whereIn('store_id', $store_ids);
        }

        $query->select('goods_category_id', DB::raw('sum(count) as count, sum(count*piece_weight) as total_piece_weight, sum(count*total_netglod_weight) as total_netglod_weight, sum(count*label_price) as total_label_price, sum(total_cost_fee) as total_cost'));
        $res = $query->groupBy('goods_category_id')->get()->toArray();
        $del_arr=[];
        foreach ($res as $v) {
            $del_arr[$v['goods_category_id']]['goods_category_id'] = $v['goods_category_id'];
            $del_arr[$v['goods_category_id']]['name'] = GoodsCategory::where(['id' => $v['goods_category_id']])->value('category_name');
            $del_arr[$v['goods_category_id']]['del'] = $v;
//            $is_gram = GoodsCategory::where(['id' => $v['goods_category_id']])->value('is_gram');
//            if ($is_gram == 2) {
//                $del_arr[$v['goods_category_id']]['del']['total_cost'] = $v['total_cost_2'];
//            } else {
//                $del_arr[$v['goods_category_id']]['del']['total_cost'] = $v['total_cost_1'];
//            }
        }


        // 补全分类下，没有数据的情况
        $cateList = GoodsCategory::query()
            ->where('site_basic_goods_category.is_del', 1)
            ->join('site_store_list', 'site_basic_goods_category.store_id', '=', 'site_store_list.id')
            ->where('site_basic_goods_category.cp_id', $accountinfo['cp_id'])
            ->when(!empty($store_ids), function ($query) use ($store_ids) {
                $query->whereIn('store_id', $store_ids);
            })
            ->when(!empty($goods_category_ids), function ($query) use ($goods_category_ids) {
                $query->whereIn('site_basic_goods_category.id', $goods_category_ids);
            })
            ->selectRaw('zxb_site_basic_goods_category.*,zxb_site_store_list.store_name as store_name')
            ->get()
            ->toArray();


        $data = [];


        foreach ($cateList as $item) {
            $start = $starts[$item['id']]['start'] ?? [];
            $end = $ends[$item['id']]['end'] ?? [];
            $stock_in = $stock_ins[$item['id']]['stock_in'] ?? [];
            $allot_in = $allot_ins[$item['id']]['allot_in'] ?? [];
            $allot_out = $allot_outs[$item['id']]['allot_out'] ?? [];
            $replenish = $replenishs[$item['id']]['replenish'] ?? [];
            $return=$returns[$item['id']]['return'] ?? [];
            $sales=$sales_arr[$item['id']]['sales'] ?? [];
            $oncounter=$oncounter_arr[$item['id']]['oncounter'] ?? [];
            $del=$del_arr[$item['id']]['del'] ?? [];

            if (!empty($start) || !empty($end) || !empty($stock_in) || !empty($allot_in) || !empty($allot_out) || !empty($replenish)||!empty($return)|| !empty($sales)||!empty($oncounter)|| !empty($del)) {
                $data[$item['id']]['goods_category_id'] = $item['id'];
                $data[$item['id']]['name'] = $item['category_name'];
                $data[$item['id']]['start'] = $start;
                $data[$item['id']]['end'] = $end;
                $data[$item['id']]['stock_in'] = $stock_in;
                $data[$item['id']]['allot_in'] = $allot_in;
                $data[$item['id']]['allot_out'] = $allot_out;
                $data[$item['id']]['replenish'] = $replenish;
                $data[$item['id']]['return'] = $return;
                $data[$item['id']]['sales'] = $sales;
                $data[$item['id']]['oncounter'] = $oncounter;
                $data[$item['id']]['del'] = $del;
            }
        }
        return array_values($data);

    }

    public function doexport(Request $request)
    {
        $list = $this->getdata($request);
        $store_id = $request['store_ids'];
        $store_id = is_array($store_id) ? $store_id : (is_array(json_decode($store_id, true)) ? json_decode($store_id, true) : []);
        $store_names = SiteStore::whereIn('id', $store_id)->pluck('store_name')->toArray();
        if (!$list) {
            return response()->json(['code' => '001', 'msg' => '导出失败']);
        }

        $row = [
            ['goods_category_id' => '进销存报表'],
            ['goods_category_id' => '所属门店：' . join(',', $store_names)],
            [
                'goods_category_id' => '类别',
                'start_count' => '期初-件数',
                'start_total_piece_weight' => '期初-总件重',
                'start_total_netglod_weight' => '期初-总金重',
                'start_total_label_price' => '期初-标签价',
                'start_total_cost' => '期初-成本',
                'stock_in_count' => '采购入库-件数',
                'stock_in_total_piece_weight' => '采购入库-总件重',
                'stock_in_total_netglod_weight' => '采购入库-总金重',
                'stock_in_total_label_price' => '采购入库-标签价',
                'stock_in_total_cost' => '采购入库-成本',
                'allot_in_count' => '调拨入库-件数',
                'allot_in_total_piece_weight' => '调拨入库-总件重',
                'allot_in_total_netglod_weight' => '调拨入库-总金重',
                'allot_in_total_label_price' => '调拨入库-标签价',
                'allot_in_total_cost' => '调拨入库-成本',
                'replenish_count' => '补货入库-件数',
                'replenish_total_piece_weight' => '补货入库-总件重',
                'replenish_total_netglod_weight' => '补货入库-总金重',
                'replenish_total_label_price' => '补货入库-标签价',
                'replenish_total_cost' => '补货入库-成本',
                'return_count' => '返厂出库-件数',
                'return_total_piece_weight' => '返厂出库-总件重',
                'return_total_netglod_weight' => '返厂出库-总金重',
                'return_total_label_price' => '返厂出库-标签价',
                'return_total_cost' => '返厂出库-成本',
                'allot_out_count' => '调拨出库-件数',
                'allot_out_total_piece_weight' => '调拨出库-总件重',
                'allot_out_total_netglod_weight' => '调拨出库-总金重',
                'allot_out_total_label_price' => '调拨出库-标签价',
                'allot_out_total_cost' => '调拨出库-成本',
                'sales_count' => '销售出库-件数',
                'sales_total_piece_weight' => '销售出库-总件重',
                'sales_total_netglod_weight' => '销售出库-总金重',
                'sales_total_label_price' => '销售出库-标签价',
                'sales_total_cost' => '销售出库-成本',
                'oncounter_count' => '上柜-件数',
                'oncounter_total_piece_weight' => '上柜-总件重',
                'oncounter_total_netglod_weight' => '上柜-总金重',
                'oncounter_total_label_price' => '上柜-标签价',
                'oncounter_total_cost' => '上柜-成本',
                'del_count' => '删除-件数',
                'del_total_piece_weight' => '删除-总件重',
                'del_total_netglod_weight' => '删除-总金重',
                'del_total_label_price' => '删除-标签价',
                'del_total_cost' => '删除-成本',
                'end_count' => '期末-件数',
                'end_total_piece_weight' => '期末-总件重',
                'end_total_netglod_weight' => '期末-总金重',
                'end_total_label_price' => '期末-标签价',
                'end_total_cost' => '期末-成本',
            ]];

        $is_store_account = StoreCommon::getIsStoreAccount($request);
        if (!$is_store_account) {
            unset($row[0]['start_total_cost']);
            unset($row[0]['stock_in_total_cost']);
            unset($row[0]['allot_in_total_cost']);
            unset($row[0]['replenish_total_cost']);
            unset($row[0]['return_total_cost']);
            unset($row[0]['allot_out_total_cost']);
            unset($row[0]['sales_total_cost']);
            unset($row[0]['oncounter_total_cost']);
            unset($row[0]['del_total_cost']);
            unset($row[0]['end_total_cost']);
        }

        $exceldata = [];
        foreach ($list as $val) {
            $exceldata[] = [
                'goods_category_id' => $val['name'],
                'start_count' => isset($val['start']['count']) ? $val['start']['count'] : '0',
                'start_total_piece_weight' => isset($val['start']['total_piece_weight']) ? $val['start']['total_piece_weight'] : '0',
                'start_total_netglod_weight' => isset($val['start']['total_netglod_weight']) ? $val['start']['total_netglod_weight'] : '0',
                'start_total_label_price' => isset($val['start']['total_label_price']) ? $val['start']['total_label_price'] / 100 : '0',
                'start_total_cost' => isset($val['start']['total_cost']) ? $val['start']['total_cost'] / 100 : '0',

                'stock_in_count' => isset($val['stock_in']['count']) ? $val['stock_in']['count'] : '0',
                'stock_in_total_piece_weight' => isset($val['stock_in']['total_piece_weight']) ? $val['stock_in']['total_piece_weight'] : '0',
                'stock_in_total_netglod_weight' => isset($val['stock_in']['total_netglod_weight']) ? $val['stock_in']['total_netglod_weight'] : '0',
                'stock_in_total_label_price' => isset($val['stock_in']['total_label_price']) ? $val['stock_in']['total_label_price'] / 100 : '0',
                'stock_in_total_cost' => isset($val['stock_in']['total_cost']) ? $val['stock_in']['total_cost'] / 100 : '0',

                'allot_in_count' => isset($val['allot_in']['count']) ? $val['allot_in']['count'] : '0',
                'allot_in_total_piece_weight' => isset($val['allot_in']['total_piece_weight']) ? $val['allot_in']['total_piece_weight'] : '0',
                'allot_in_total_netglod_weight' => isset($val['allot_in']['total_netglod_weight']) ? $val['allot_in']['total_netglod_weight'] : '0',
                'allot_in_total_label_price' => isset($val['allot_in']['total_label_price']) ? $val['allot_in']['total_label_price'] / 100 : '0',
                'allot_in_total_cost' => isset($val['allot_in']['total_cost']) ? $val['allot_in']['total_cost'] / 100 : '0',

                'replenish_total_piece_weight' => isset($val['replenish']['total_piece_weight']) ? $val['replenish']['total_piece_weight'] : '0',
                'replenish_total_netglod_weight' => isset($val['replenish']['total_netglod_weight']) ? $val['replenish']['total_netglod_weight'] : '0',
                'replenish_total_label_price' => isset($val['replenish']['total_label_price']) ? $val['replenish']['total_label_price'] / 100 : '0',
                'replenish_total_cost' => isset($val['replenish']['total_cost']) ? $val['replenish']['total_cost'] / 100 : '0',
                'replenish_count' => isset($val['replenish']['count']) ? $val['replenish']['count'] : '0',


                'return_count' => isset($val['return']['count']) ? $val['return']['count'] : '0',
                'return_total_piece_weight' => isset($val['return']['total_piece_weight']) ? $val['return']['total_piece_weight'] : '0',
                'return_total_netglod_weight' => isset($val['return']['total_netglod_weight']) ? $val['return']['total_netglod_weight'] : '0',
                'return_total_label_price' => isset($val['return']['total_label_price']) ? $val['return']['total_label_price'] / 100 : '0',
                'return_total_cost' => isset($val['return']['total_cost']) ? $val['return']['total_cost'] / 100 : '0',

                'allot_out_count' => isset($val['allot']['count']) ? $val['allot']['count'] : '0',
                'allot_out_total_piece_weight' => isset($val['allot']['total_piece_weight']) ? $val['allot']['total_piece_weight'] : '0',
                'allot_out_total_netglod_weight' => isset($val['allot']['total_netglod_weight']) ? $val['allot']['total_netglod_weight'] : '0',
                'allot_out_total_label_price' => isset($val['allot']['total_label_price']) ? $val['allot']['total_label_price'] / 100 : '0',
                'allot_out_total_cost' => isset($val['allot']['total_cost']) ? $val['allot']['total_cost'] / 100 : '0',

                'sales_count' => isset($val['sales']['count']) ? $val['sales']['count'] : '0',
                'sales_total_piece_weight' => isset($val['sales']['total_piece_weight']) ? $val['sales']['total_piece_weight'] : '0',
                'sales_total_netglod_weight' => isset($val['sales']['total_netglod_weight']) ? $val['sales']['total_netglod_weight'] : '0',
                'sales_total_label_price' => isset($val['sales']['total_label_price']) ? $val['sales']['total_label_price'] / 100 : '0',
                'sales_total_cost' => isset($val['sales']['total_cost']) ? $val['sales']['total_cost'] / 100 : '0',

                'oncounter_count' => isset($val['oncounter']['count']) ? $val['oncounter']['count'] : '0',
                'oncounter_total_piece_weight' => isset($val['oncounter']['total_piece_weight']) ? $val['oncounter']['total_piece_weight'] : '0',
                'oncounter_total_netglod_weight' => isset($val['oncounter']['total_netglod_weight']) ? $val['oncounter']['total_netglod_weight'] : '0',
                'oncounter_total_label_price' => isset($val['oncounter']['total_label_price']) ? $val['oncounter']['total_label_price'] / 100 : '0',
                'oncounter_total_cost' => isset($val['oncounter']['total_cost']) ? $val['oncounter']['total_cost'] / 100 : '0',

                'del_count' => isset($val['del']['count']) ? $val['del']['count'] : '0',
                'del_total_piece_weight' => isset($val['del']['total_piece_weight']) ? $val['del']['total_piece_weight'] : '0',
                'del_total_netglod_weight' => isset($val['del']['total_netglod_weight']) ? $val['del']['total_netglod_weight'] : '0',
                'del_total_label_price' => isset($val['del']['total_label_price']) ? $val['del']['total_label_price'] / 100 : '0',
                'del_total_cost' => isset($val['del']['total_cost']) ? $val['del']['total_cost'] / 100 : '0',

                'end_count' => isset($val['end']['count']) ? $val['end']['count'] : '0',
                'end_total_piece_weight' => isset($val['end']['total_piece_weight']) ? $val['end']['total_piece_weight'] : '0',
                'end_total_netglod_weight' => isset($val['end']['total_netglod_weight']) ? $val['end']['total_netglod_weight'] : '0',
                'end_total_label_price' => isset($val['end']['total_label_price']) ? $val['end']['total_label_price'] / 100 : '0',
                'end_total_cost' => isset($val['end']['total_cost']) ? $val['end']['total_cost'] / 100 : '0',
            ];
        }
        //所有的导出改到这里了--2024-10-22 12：12
        $filename = '进销存报表导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }
}
